<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
$return['post']=array();
$return['paytypes']=array(
		'credit'=>'余额支付',
		'wechat'=>'微信支付',
		'alipay'=>'支付宝支付',
		'yinlian'=>'银联支付',
		'hdfk'=>'货到付款',
);
if($_RQ['op']=='main'){
    $return['apply_status']=$this->apply_status;
	exi($return);
}
/*******op*******/
set_html('title','配送订单');
if($_RQ['op']=='list'){
    $return['apply_status']=$this->apply_status;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status']!=100){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}elseif($_RQ['status']==100){
		$return['status']=100;
	}else{
		$params['status']=1;
		$return['status']=1;
	}
	if($_RQ['today']){
		$params['createtime >']=strtotime(date('ymd'));
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$order_list=pdo_getslice('jy_liangoods_sendlog',$params,array($pindex,$psize),$total,array(),'','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$store=pdo_get('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']));	
			$item['store']=$store;
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
            $order=pdo_get('jy_weishop_order',array('id'=>$item['order_id'],'uniacid'=>$_SESSION['uniacid']),array('is_peisong','paytype','ordersn','ispay','addressid'));
            if($order['is_peisong']==1){
                $item['sendtype']=4;
            }
            $item['sendtypetitle']=$this->send_types[$item['sendtype']];
			$item['ordersn']=$order['ordersn'];
			$item['paytype']=$order['paytype'];
			$item['ispay']=$order['ispay'];
			$item['statustitle']=$this->sendlog_status[$item['status']];
            $item['images']=json_decode($item['images'],true);
			//用户地址
			$address['pcd']=json_decode($item['pcd'],true);
			$address['name']=$item['name'];
			$address['mobile']=$item['mobile'];
			$item['order_out']=$address;
			if($item['goods']){
				$item['goods']=json_decode($item['goods'],true);
			}
			$selfinfo=pdo_get('jy_weishop_order_self',array('orderid'=>$item['id']));
			$selfinfo['selftime']=$selfinfo['selftime']?date('Y-m-d H:i:s',$selfinfo['selftime']):'尽快配送';
			$selfinfo['selftime']=$selfinfo['out_time']?:$selfinfo['selftime'];
			$selfinfo['number2']=substr($selfinfo['number'],-4);
			$item['selfinfo']=$selfinfo;
			$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
			$item['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		}
	}
	$return['list']=$return['order_list']=$order_list;
	$return['isopenvoice']=$this->session('isopenvoice')?1:0;
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_liangoods_sendlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['storeid']));	
	$store_times=json_decode($store['serve_time'],true);
	$order['serve_time']=$store_times[$order['timeid']];
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	if(!is_array($address['pcd'])){
		$address['pcd']=json_decode($address['pcd'],true);
	}
	$address['name']=$address['name']?:$address['realname'];
	$order['order_out']=$address;
	$order['paytime']=$order['paytime']?date('Y-m-d H:i:s', $order['paytime']):'';
	//自定义字段数据
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['store']=$store;
	exi($return);
}
if($_RQ['op']=='check'){
    $post=get_postdata();
    $log_id=intval($post['id']);
    $status=intval($post['status']);
    if(!$status){
        exi('请选择审核通过,还是不通过！','error');
    }
    $this->model('merch')->check_sendlog($log_id,$status,$post['remark']);
    exi('处理成功！','','/manage.worker_order/list/');
}
if($_RQ['op']=='getnew'){
    $params=array(
        'status'=>2,
        'uniacid'=>$_SESSION['uniacid'],
    );
    $this->session('isopenvoice',1);
    $order=pdo_get('jy_liangoods_sendlog',$params);
    if($order['id']){
        $last_noticetime= $this->session('last_noticetime');
	    if(!$last_noticetime || $last_noticetime<time()-60){
            $this->model('notice')->notice_newget($order['id'],$_SESSION['uid']);
            $this->session('last_noticetime',time());
        }
        exi($order);
    }else{
        exi('none');
    }
}